package com.google.code.burner.pomodoro;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

public class MainFrame extends javax.swing.JFrame {

	private static final long serialVersionUID = 2238772827359628414L;
	private static final int W_WIDTH = 400;
	private static final int W_HEIHGT = 200;

	private ActionListener actionListener;

	// Window components
	private JPanel toolbar;
	private JPanel timerLabel;
	private JButton startTimer;
	private JButton stopTimer;
	private JButton startSmallBreak;
	private JButton startLargeBreak;
	private JLabel timerStatus;

	public MainFrame(final SimplePomodoroTimer parent) {
		super();
		// Set default look and feel
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {

		}

		// Window size and location centered on the screen
		setSize(new Dimension(W_WIDTH, W_HEIHGT));
		setResizable(false);
		setLocationRelativeTo(null);

		// Window title
		setTitle("SimplePomodoroTimer");

		// Close operation (close button click)
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		// Listener
		actionListener = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				switch (e.getActionCommand()) {
				case "Pomodoro":
					parent.startPomodoro();
					startTimer.setEnabled(false);
					startSmallBreak.setEnabled(false);
					startLargeBreak.setEnabled(false);
					stopTimer.setEnabled(true);
					break;
				case "Stop":
					parent.setActive(false);
					stopTimer.setEnabled(false);
					startTimer.setEnabled(true);
					startSmallBreak.setEnabled(true);
					startLargeBreak.setEnabled(true);
					break;
				case "Kleine Pause":
					parent.startSmallBreak();
					startTimer.setEnabled(false);
					startSmallBreak.setEnabled(false);
					startLargeBreak.setEnabled(false);
					stopTimer.setEnabled(true);
					break;
				case "Große Pause":
					parent.startLargeBreak();
					startTimer.setEnabled(false);
					startSmallBreak.setEnabled(false);
					startLargeBreak.setEnabled(false);
					stopTimer.setEnabled(true);
					break;
				default:
					System.out.println("unsupported action performed");
				}
			}
		};

		// Window components
		JPanel container = new JPanel();
		container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
		container.add(toolbar());
		container.add(timerLabel());

		getContentPane().add(container);

	}

	private JPanel timerLabel() {
		timerLabel = new JPanel();

		timerStatus = new JLabel();
		timerStatus.setText("0");
		Font font = new Font(Font.SANS_SERIF,Font.BOLD,50);
		timerStatus.setFont(font);
		timerStatus.setBorder(new EmptyBorder(20,20,20,20));
		timerLabel.add(timerStatus);

		return timerLabel;
	}

	private JPanel toolbar() {
		toolbar = new JPanel();
		toolbar.setLayout(new BoxLayout(toolbar, BoxLayout.X_AXIS));

		startTimer = new JButton("Pomodoro");
		startTimer.addActionListener(actionListener);
		toolbar.add(startTimer);

		startSmallBreak = new JButton("Kleine Pause");
		startSmallBreak.addActionListener(actionListener);
		toolbar.add(startSmallBreak);

		startLargeBreak = new JButton("Große Pause");
		startLargeBreak.addActionListener(actionListener);
		toolbar.add(startLargeBreak);

		stopTimer = new JButton("Stop");
		stopTimer.addActionListener(actionListener);
		toolbar.add(stopTimer);
		
		return toolbar;
	}

	public void setLabel(long remaining) {
		int seconds = Math.round(remaining/1000);
		int minutes = (int) Math.floor(seconds/60);
		seconds = seconds - minutes*60;
		timerStatus.setText(String.valueOf(minutes)+":"+String.valueOf(seconds));
	}
}
